/**
 * 统一日志管理器
 * 提供统一的日志输出接口，所有组件的日志都通过此管理器输出到VSCode控制台
 * @author lihengtt <lihengsir@gmail.com>
 * @github https://github.com/lihengtt
 */
import * as vscode from 'vscode';

export class LogManager {
    private static instance: LogManager;
    private outputChannel: vscode.OutputChannel;
    private debugMode: boolean = true;

    private constructor() {
        this.outputChannel = vscode.window.createOutputChannel('Rsync Deployment');
    }

    /**
     * 获取LogManager单例实例
     * @returns LogManager实例
     * @author lihengtt <lihengsir@gmail.com>
     */
    public static getInstance(): LogManager {
        if (!LogManager.instance) {
            LogManager.instance = new LogManager();
        }
        return LogManager.instance;
    }

    /**
     * 输出信息日志
     * @param message - 日志消息
     * @param component - 组件名称
     * @author lihengtt <lihengsir@gmail.com>
     */
    public info(message: string, component?: string): void {
        const timestamp = new Date().toLocaleTimeString();
        const logMessage = component 
            ? `[${timestamp}] [${component}] ${message}`
            : `[${timestamp}] ${message}`;
        
        this.outputChannel.appendLine(logMessage);
        
        if (this.debugMode) {
            console.log(logMessage);
        }
    }

    /**
     * 输出错误日志
     * @param message - 错误消息
     * @param component - 组件名称
     * @param error - 错误对象
     * @author lihengtt <lihengsir@gmail.com>
     */
    public error(message: string, component?: string, error?: any): void {
        const timestamp = new Date().toLocaleTimeString();
        const errorMessage = error ? `${message}: ${error}` : message;
        const logMessage = component 
            ? `[${timestamp}] [ERROR] [${component}] ${errorMessage}`
            : `[${timestamp}] [ERROR] ${errorMessage}`;
        
        this.outputChannel.appendLine(logMessage);
        
        if (this.debugMode) {
            console.error(logMessage);
        }
    }

    /**
     * 输出警告日志
     * @param message - 警告消息
     * @param component - 组件名称
     * @author lihengtt <lihengsir@gmail.com>
     */
    public warn(message: string, component?: string): void {
        const timestamp = new Date().toLocaleTimeString();
        const logMessage = component 
            ? `[${timestamp}] [WARN] [${component}] ${message}`
            : `[${timestamp}] [WARN] ${message}`;
        
        this.outputChannel.appendLine(logMessage);
        
        if (this.debugMode) {
            console.warn(logMessage);
        }
    }

    /**
     * 输出调试日志
     * @param message - 调试消息
     * @param component - 组件名称
     * @author lihengtt <lihengsir@gmail.com>
     */
    public debug(message: string, component?: string): void {
        if (!this.debugMode) {
            return;
        }
        
        const timestamp = new Date().toLocaleTimeString();
        const logMessage = component 
            ? `[${timestamp}] [DEBUG] [${component}] ${message}`
            : `[${timestamp}] [DEBUG] ${message}`;
        
        this.outputChannel.appendLine(logMessage);
        console.log(logMessage);
    }

    /**
     * 显示输出控制台
     * @author lihengtt <lihengsir@gmail.com>
     */
    public show(): void {
        this.outputChannel.show();
    }

    /**
     * 清空日志
     * @author lihengtt <lihengsir@gmail.com>
     */
    public clear(): void {
        this.outputChannel.clear();
    }

    /**
     * 设置调试模式
     * @param enabled - 是否启用调试模式
     * @author lihengtt <lihengsir@gmail.com>
     */
    public setDebugMode(enabled: boolean): void {
        this.debugMode = enabled;
    }

    /**
     * 记录连接事件
     * @param host - 主机地址
     * @param port - 端口
     * @param username - 用户名
     * @param success - 是否成功
     * @author lihengtt <lihengsir@gmail.com>
     */
    public logConnection(host: string, port: number, username: string, success: boolean): void {
        const status = success ? '成功' : '失败';
        this.info(`SSH连接${status}: ${username}@${host}:${port}`, 'Connection');
    }

    /**
     * 记录文件操作事件
     * @param operation - 操作类型
     * @param localPath - 本地路径
     * @param remotePath - 远程路径
     * @param success - 是否成功
     * @author lihengtt <lihengsir@gmail.com>
     */
    public logFileOperation(operation: string, localPath: string, remotePath: string, success: boolean): void {
        const status = success ? '成功' : '失败';
        this.info(`${operation}${status}: ${localPath} -> ${remotePath}`, 'FileOperation');
    }
}